<template>
  <el-dialog v-dialogDrag v-if="popenevTVisible" title="列表字段" :close-on-click-modal="false" :before-close="handleCancel" :visible.sync="popenevTVisible" width="500px">
    <div class="header">
      <el-input v-model="keyword" placeholder="搜索字段" suffix-icon="el-icon-search" @change="search"></el-input>
      <div class="switch">
        <el-switch
          v-model="isAll"
          @change="changeAllSwitch"
          active-color="#13ce66"
          inactive-color="#e2e2e3">
        </el-switch>
        <span>显示隐藏字段</span>
      </div>
    </div>
    <el-row class="columns-header">
      <el-col :span="3" class="tlc">操作</el-col>
      <el-col :span="5" class="tlc">显示/隐藏</el-col>
      <el-col :span="6" class="tlc">字段名称</el-col>
      <el-col :span="10" style="text-align: right;padding-right: 80px;">序号</el-col>
    </el-row>
    <draggable
      v-model="dataList"
      group="people"
      @change="change"
      @start="start"
      @end="end"
    >
      <transition-group v-loading="loading">
        <el-row v-for="(item, index) in dataList" :key="item.key" class="item" :class="{ 'active': index % 2 != 0 }">
          <template v-if="item.type != 'ACTION'">
            <el-col :span="3" class="tlc"><i class="el-icon-more action"></i></el-col>
            <el-col :span="5" class="tlc">
              <el-switch
                v-model="item.visible"
                active-color="#13ce66"
                inactive-color="#e2e2e3">
              </el-switch>
            </el-col>
            <el-col :span="6" class="tlc">{{ item.title }}</el-col>
            <el-col :span="10" class="xh-input">
              <input type="text" v-model="dataList[index].xh"  @change="changeSort($event, item)">
              <!-- <el-input v-model="dataList[index].xh"  @change="changeSort($event, item)"></el-input> -->
            </el-col>
          </template>
        </el-row>
      </transition-group>
    </draggable>
    <template slot="footer">
      <el-button @click="handleCancel"> 取消 </el-button>
      <el-button type="primary" @click="handleSure"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script>
import draggable from "vuedraggable";
export default {
  components: {
    draggable
  },
  data () {
    return {
      popenevTVisible: false,
      rawData: [],
      // 视图展示字段数据
      columns: [],
      // 视图所有字段数据
      allColumns: [],
      dataList: [],
      //promise 状态
      promiseStatus: null,
      keyword: '',
      isAll: true,
      // 当前拖到的对象
      movedObj: {},
      loading: false
    }
  },
  methods: {
    // 打开弹窗
    show(data, config)
    {
      this.columns = data.columns || []
      this.allColumns = data.allColumns || []
      this.columns.map((item, i) => {
        item.xh = i + 1
      })
      this.allColumns.map((item,i) => {
        item.xh = i + 1
      })
      if (this.isAll)
      {
        this.dataList = this.allColumns
      }
      this.rawData = this.allColumns;
      this.popenevTVisible = true;
      // 创建promise
      return new Promise((resolve, reject) => {
        this.promiseStatus = {
          resolve,
          reject
        }
      })
    },
    // 取消
    handleCancel()
    {
      this.popenevTVisible = false;
      // 清空勾选项
      this.promiseStatus.reject({
        msg: "取消"
      })
    },
    // 监听拖拽
    change(event)
    {
      console.log(event, '监听拖拽')
      this.movedObj = event.moved.element
    },
    // 开始拖拽
    start(event)
    {
      console.log(event, '开始拖拽')
    },
    // 结束拖拽
    end(event)
    {
      console.log(event, '结束拖拽')
      this.loading = true
      this.dataList.map((item, i) => {
        item.xh = i + 1
      })
      setTimeout(() => {
        this.loading = false
      }, 1000)
      console.log(this.dataList, 'dataList')
    },
    // 确定
    handleSure()
    {
      console.log(this.allColumns, '确定')
      console.log(this.dataList)
      // return
      this.promiseStatus.resolve({
        resultData: this.dataList
      });
      this.popenevTVisible = false
    },
    // 搜索
    search(value)
    {
      console.log(value, '搜索')
      if (value)
      {
        var searchList = new Array()
        var columns = this.isAll ? this.allColumns : this.columns
        columns.map(item => {
          if (item.title.indexOf(value) != -1)
          {
            searchList.push(item)
          }
        })
        this.dataList = searchList
      }
      else
      {
        this.dataList = this.rawData
      }
    },
    // 修改序号排序
    changeSort(P_Event, item)
    {
      this.dataList = this.dataList.sort((a,b) => {
        return (a.xh - b.xh)
      })
      var preIndex = this.dataList.findIndex(v => {
        return v.xh == Number(P_Event.target.value)
      })
      var nextIndex = this.rawData.findIndex(v => {
        return v.key == item.key
      })
      // this.columns = this.columns.splice(nextIndex, 1, ...this.columns.splice(preIndex, 1, this.columns[nextIndex]));
      this.dataList.splice(nextIndex, 1, ...this.dataList.splice(preIndex, 1, this.dataList[nextIndex]));
      for (let i = preIndex; i < this.dataList.length; i++)
      {
        this.dataList[i].xh = i + 1
      }
    },
    // 显示隐藏所有字段
    changeAllSwitch(P_Event)
    {
      if (this.isAll)
      {
        this.dataList = this.allColumns
      }
      else
      {
        this.dataList = this.columns
        this.dataList.map((item, i) => {
          item.xh = i + 1
        })
        console.log(this.dataList, '------')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.header {
  display: flex;
  align-items: center;
  line-height: 36px;
  .el-input {
    flex: 2;
    margin-right: 30px;
  }
  ::v-deep .el-input__inner {
    border-radius: 20px;
    height: 30px;
    line-height: 30px;
    font-size: 12px;
  }
  .switch {
    display: flex;
    align-items: center;
    .el-switch {
      margin-right: 5px;
    }
  }
}
.columns-header {
  height: 36px;
  line-height: 36px;
  background: #f2f2f2;
  font-size: 14px;
  color: #333;
  font-weight: bold;
  margin-top: 15px;
  width: 100%;
}
.action {
  transform: rotate(90deg);
  font-size: 18px;
}
.xh-input {
  text-align: right;
  padding-right: 20px;
  input {
    height: 30px;
    line-height: 30px;
    width: 120px;
    border-radius: 5px;
    border: 1px solid #ddd;
    padding-left: 10px;
  }
  .el-input {
    width: 100px;
    ::v-deep .el-input__inner {
      height: 30px;
      line-height: 30px;
    }
  }
}
.tlc {
  text-align: center;
}
.item {
  line-height: 36px;
  cursor: move;
  background: #fff;
}
.active {
  background: #f2f2f2;
}
::v-deep .el-dialog__footer {
  text-align: center !important;
}
</style>